﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Store
{
    public partial class frmSampleBuyerOperation : Form
    {

        Entities db = new Entities();

        List<T_Sample> TSample = null;

        private bool isEnd = false;
        T_Sample m_Sample = null;
        T_LendDetail m_LendDetial = null;
        T_Lend m_Lend = null;

        T_Flow m_Flow = null;

        public List<int> xID
        {
            get;
            set;
        }
        //public ListView lv
        //{
        //    set
        //    {
        //        if (value.Items.Count <= 0) return;

        //        TSample = new List<T_Sample>();
        //        foreach (ListViewItem lvItem in value.Items)
        //        {
        //            var list = db.T_Sample.Where(a => a.productNo == lvItem.Text && a.sKind == sKind);
        //            foreach (T_Sample sample in list.Where(a => a.bCount > 0))
        //            {
        //                TSample.Add(sample);
        //            }
        //        }
        //        bs.DataSource = TSample;


        //    }
        //}
        public frmSampleBuyerOperation()
        {
            InitializeComponent();
            Service.CommandName(cbCommandName);
        }

        private void SetColumn()
        {
            int i = 0;
            foreach (var list in bs)
            {
                int? n = 0;
                gv1.Rows[i].Cells["VlCount"].Value = gv1.Rows[i].Cells["lCount"].Value;
                gv1.Rows[i].Cells[9].Value = n;
                gv1.Rows[i].Cells[10].Value = n;
                i++;
            }
        }

        private void frmSampleBuyerOperation_Load(object sender, EventArgs e)
        {
            if (xID.Count <= 0) return;

            TSample = new List<T_Sample>();
            for (int i = 0; i < xID.Count; i++)
            {
                var id = xID[i];
                var list = db.T_Sample.Join(db.T_LendDetail, b => b.xID, c => c.xID, (b, c) => new { b, c }).Join(db.T_Lend, w => w.c.lID, f => f.lID, (w, f) => new { w.b.xID, w.b.productNo, w.b.contractNo, w.b.opDate, w.b.size2, w.b.property, w.b.sKind, w.b.brand, w.b.factory, w.b.allCount, w.b.pCount, w.b.sCount, w.c.lCount, w.b.location, w.c.commandName, f.lendTime, w.b.custDept, w.b.reMark, w.c.lReturn, w.c.lendToOut,f.lID,w.c.ldID }).Where(a => a.xID == id && a.lCount > (a.lReturn + a.lendToOut)).ToList();
                bs.Add(list.FirstOrDefault());
            }
            //bs.DataSource = TSample;

            SetColumn();

            isEnd = true;
            Service.CommandName(cbCommandName);
        }

        private void gv1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (isEnd)
            {
                if (gv1.Columns[e.ColumnIndex].Name.Equals("bzReturn") || gv1.Columns[e.ColumnIndex].Name.Equals("loantoOut"))
                {
                    try
                    {
                        if (e.RowIndex < 0) return;
                        DataGridViewCell cell = gv1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        if (cell != null)
                        {
                            if (Convert.ToInt32(cell.Value) > Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["lCount"].Value))
                            {
                                Msg.ShowWarning(gv1.Columns[cell.ColumnIndex].HeaderText + "大于借出数量");

                                gv1.Rows[e.RowIndex].Cells["VlCount"].Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["lCount"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["bzReturn"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["loantoOut"].Value);
                                cell.Value = 0;
                                return;
                            }
                            else
                            {
                                gv1.Rows[e.RowIndex].Cells["VlCount"].Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["lCount"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["bzReturn"].Value) - Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["loantoOut"].Value);

                            }
                            if (Convert.ToInt32(this.gv1.Rows[e.RowIndex].Cells["bzReturn"].Value.ToString()) +
                                Convert.ToInt32(this.gv1.Rows[e.RowIndex].Cells["loantoOut"].Value.ToString())
                                > Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["lCount"].Value))
                            {
                                Msg.ShowWarning("操作数量大于借出数量");
                                gv1.Rows[e.RowIndex].Cells["VlCount"].Value = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells["lCount"].Value);
                                this.gv1.Rows[e.RowIndex].Cells["bzReturn"].Value = 0;
                                this.gv1.Rows[e.RowIndex].Cells["loantoOut"].Value = 0;
                                return;
                            }
                        }
                    }
                    catch (Exception a)
                    {
                        Msg.ShowWarning(a.Message);
                    }
                }
            }
        }

        private void btnSetOperation_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbCommandName.Text))
            {
                Msg.ShowInfo("请选择指令人");
                return;
            }
            try
            {
                if (this.gv1.EndEdit())
                {
                    for (int i = 0; i < gv1.RowCount; i++)
                    {
                        if (gv1.Rows[i].Cells["bzReturn"].Value.ToString() == "0" && gv1.Rows[i].Cells["loantoOut"].Value.ToString() == "0" )
                        {
                            Msg.ShowWarning("请设置" + gv1.Rows[i].Cells["colProductNo"].Value.ToString() + "的操作数量！");
                            return;
                        }
                    }
                    for (int z = 0; z < gv1.RowCount;z++ )
                    {
                        int index = gv1.CurrentRow.Index;
                        int xid = Convert.ToInt32(gv1.Rows[index].Cells["colxID"].Value.ToString());
                        int lid = Convert.ToInt32(gv1.Rows[index].Cells["collID"].Value.ToString());
                        int ldid = Convert.ToInt32(gv1.Rows[index].Cells["colldID"].Value.ToString());
                        var xlist = db.T_Sample.Where(a => a.xID == xid);
                        var llist = db.T_Lend.Where(a => a.lID == lid);
                        var ldlist = db.T_LendDetail.Where(a => a.ldID == ldid);
                        foreach (var sample in xlist)
                        {
                            m_Sample = sample;
                            break;
                        }
                        foreach (var lend in llist)
                        {
                            m_Lend = lend;
                            break;
                        }
                        foreach (var lenddetail in ldlist)
                        {
                            m_LendDetial = lenddetail;
                            break;
                        }
                        if (Convert.ToInt32(gv1.Rows[z].Cells["bzReturn"].Value.ToString()) > 0)
                        {
                            m_Sample.bCount -= Convert.ToInt32(gv1.Rows[z].Cells["bzReturn"].Value.ToString());
                            m_LendDetial.lReturn += Convert.ToInt32(gv1.Rows[z].Cells["bzReturn"].Value.ToString());

                            m_Flow = new T_Flow();
                            if (db.T_Flow.Count() <= 0) m_Flow.cID = 1;
                            else m_Flow.cID = db.T_Flow.OrderByDescending(a => a.cID).First().cID + 1;
                            m_Flow.xID = m_Sample.xID;
                            m_Flow.sCount = Convert.ToInt32(gv1.Rows[z].Cells["bzReturn"].Value.ToString());
                            m_Flow.reason = "归还";
                            m_Flow.opName = Startup.GetApp().CurrentUser.name;
                            m_Flow.opDate = DateTime.Now.ToLocalTime();
                            m_Flow.commandName = cbCommandName.Text;
                            db.T_Flow.Add(m_Flow);
                            db.SaveChanges();
                        }
                        if (Convert.ToInt32(gv1.Rows[z].Cells["loantoOut"].Value.ToString()) > 0)
                        {
                            m_Sample.oCount += Convert.ToInt32(gv1.Rows[z].Cells["loantoOut"].Value.ToString());
                            m_Sample.bCount -= Convert.ToInt32(gv1.Rows[z].Cells["loantoOut"].Value.ToString());
                            m_LendDetial.lendToOut += Convert.ToInt32(gv1.Rows[z].Cells["loantoOut"].Value.ToString());

                            m_Flow = new T_Flow();
                            if (db.T_Flow.Count() <= 0) m_Flow.cID = 1;
                            else m_Flow.cID = db.T_Flow.OrderByDescending(a => a.cID).First().cID + 1;
                            m_Flow.xID = m_Sample.xID;
                            m_Flow.sCount = Convert.ToInt32(gv1.Rows[z].Cells["loantoOut"].Value.ToString());
                            m_Flow.reason = "借出转出仓";
                            m_Flow.opName = Startup.GetApp().CurrentUser.name;
                            m_Flow.opDate = DateTime.Now.ToLocalTime();
                            m_Flow.commandName = cbCommandName.Text;
                            db.T_Flow.Add(m_Flow);
                            db.SaveChanges();
                        }
                        m_Sample.sCount += Convert.ToInt32(gv1.Rows[z].Cells["bzReturn"].Value.ToString());
                        db.SaveChanges();
                    }

                    Msg.ShowInfo("完成归还操作！");

                    DialogResult = System.Windows.Forms.DialogResult.OK;

                    this.Close();

                }
                else
                {
                    Msg.ShowWarning("请结束编辑！");
                }
            }
            catch (Exception x)
            {
                Msg.ShowError(x.Message);
            }
        }

        //全部归还
        private void btnAllReturn_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gv1.RowCount; i++)
            {
                gv1.Rows[i].Cells["bzReturn"].Value = gv1.Rows[i].Cells["VlCount"].Value;
                gv1.Rows[i].Cells["VlCount"].Value = 0;
            }
        }

        //全部出仓
        private void btnAllOut_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gv1.RowCount; i++)
            {
                gv1.Rows[i].Cells["loantoOut"].Value = gv1.Rows[i].Cells["VlCount"].Value;
                gv1.Rows[i].Cells["VlCount"].Value = 0;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
